{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73bd968b-d970-4a05-94ef-4e7abf990827",
   "metadata": {},
   "source": [
    "Chapter 04\n",
    "\n",
    "# 矩阵加减法\n",
    "Book_4《矩阵力量》 | 鸢尾花书：从加减乘除到机器学习 (第二版)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a8042cab-e99c-4d94-a9be-96175baa837e",
   "metadata": {},
   "source": [
    "该代码定义了两个 $2 \\times 2$ 矩阵 $A$ 和 $B$，分别为：\n",
    "\n",
    "$$\n",
    "A = \\begin{bmatrix} 1 & 2 \\\\ 3 & 4 \\end{bmatrix}, \\quad B = \\begin{bmatrix} 2 & 6 \\\\ 4 & 8 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "代码随后计算了矩阵 $A$ 和 $B$ 的加法与减法。矩阵加法 $A + B$ 的结果为：\n",
    "\n",
    "$$\n",
    "A + B = \\begin{bmatrix} 1+2 & 2+6 \\\\ 3+4 & 4+8 \\end{bmatrix} = \\begin{bmatrix} 3 & 8 \\\\ 7 & 12 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "矩阵减法 $A - B$ 的结果为：\n",
    "\n",
    "$$\n",
    "A - B = \\begin{bmatrix} 1-2 & 2-6 \\\\ 3-4 & 4-8 \\end{bmatrix} = \\begin{bmatrix} -1 & -4 \\\\ -1 & -4 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "代码展示了使用 `np.add` 和 `np.subtract` 函数以及直接使用操作符完成矩阵加减法的两种实现方式。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05fbf711-6eee-49a5-a5a5-a00c9726e60e",
   "metadata": {},
   "source": [
    "## 导入所需库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ced7d4cd-a41e-4a61-b8eb-a5221ac81fcf",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np  # 导入NumPy库，用于数值计算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2afe209-4fad-4614-9697-2c8dcee0ac98",
   "metadata": {},
   "source": [
    "## 定义矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7c955d5b-6e3b-4ae8-b162-16e5b739b31a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1, 2],\n",
       "        [3, 4]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.matrix([[1, 2], \n",
    "               [3, 4]])  # 定义矩阵A\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7d16a094-2bae-46e1-b98c-35e1a1cbabba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[2, 6],\n",
       "        [4, 8]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B = np.matrix([[2, 6], \n",
    "               [4, 8]])  # 定义矩阵B\n",
    "B"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "83b0d359-e7e0-45ed-9241-b5643ec9a631",
   "metadata": {},
   "source": [
    "## 矩阵加法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "476f434f-f285-47f2-b444-240bf970142d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 3,  8],\n",
       "        [ 7, 12]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_plus_B = np.add(A, B)  # 使用np.add函数计算矩阵A和B的和\n",
    "A_plus_B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "dbeb5ab0-70f7-4c11-9ba4-86fceeade833",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 3,  8],\n",
       "        [ 7, 12]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_plus_B_2 = A + B  # 使用加法操作符计算矩阵A和B的和\n",
    "A_plus_B_2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d40792e-8cd1-4d9a-a372-38bf758f6f6e",
   "metadata": {},
   "source": [
    "## 矩阵减法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d1535c49-e64c-4503-8655-405263614ae6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[-1, -4],\n",
       "        [-1, -4]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_minus_B = np.subtract(A, B)  # 使用np.subtract函数计算矩阵A和B的差\n",
    "A_minus_B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "32372955-1f18-4c2f-bcb7-fc734e608328",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[-1, -4],\n",
       "        [-1, -4]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_minus_B_2 = A - B  # 使用减法操作符计算矩阵A和B的差\n",
    "A_minus_B_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
